/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * ForwardStoreDataImporter.java
 * business
 * 2012-10-18上午10:41:46
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.report.data;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.ForwardStore;
import org.jackysoft.entity.Product;
import org.jackysoft.service.DepartmentService;
import org.jackysoft.service.ProductService;
import org.jackysoft.util.ExcelUtil;
import org.jackysoft.util.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class ForwardStoreDataImporter extends AbstractDataImporter {

	static final Log logger = LogFactory.getLog(ForwardStoreDataImporter.class);
	private Integer year;
	private Integer month;
	private ProductService productService;
	private DepartmentService deptService;
	
	@Override
	public Collection<ForwardStore> fromStream(InputStream input) {
	    Collection<ForwardStore> forwards = new ArrayList<ForwardStore>();
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(input);
			int sheetCount = workbook.getNumberOfSheets();

			for (int i = 0; i < sheetCount; i++) {

				HSSFSheet sheet = workbook.getSheetAt(i);
				String sname = sheet.getSheetName();
				Department dept = deptService.getByName(sname);								
				Iterator<Row> rows = sheet.rowIterator();
				int rowIndex = 0;
				while(rows.hasNext()){
					Row row = rows.next();
					rowIndex = row.getRowNum();
					Iterator<Cell> cells = row.cellIterator();
					Cell first = ExcelUtil.nextCell(cells);
					String code = ExcelUtil.parseCode(first);
					if(StringUtils.isEmpty(code))break;
					Product pdt = productService.get(code);					
					if(pdt==null){
						logger.warn("第"+rowIndex+"行,产品"+ExcelUtil.cellString(first)+" 不存在,请和产品数据库对照后再导入");
					    continue;
					}
					
					ForwardStore fd = new ForwardStore();
					fd.setOwner(pdt);
					fd.setDepartment(dept);
					//跳过单位列
					ExcelUtil.nextCell(cells);
					//年初
					Cell third  = ExcelUtil.nextCell(cells);
				    Double yval = ExcelUtil.cellNumber(third);
					//月初
				    Cell fourth = ExcelUtil.nextCell(cells);
					Double mval = ExcelUtil.cellNumber(fourth);
					fd.setYearValue(yval);
					fd.setMonthValue(mval);
					fd.setYear(year);
					fd.setMonth(month);
					logger.info("检测到结存:"+fd);
					forwards.add(fd);
					
				}
				
			}
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
		}		
		return forwards;
	}


	@Resource
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
   

	public Integer getYear() {
		return year;
	}


	public void setYear(Integer year) {
		this.year = year;
	}


	public Integer getMonth() {
		return month;
	}


	public void setMonth(Integer month) {
		this.month = month;
	}


	@Resource
	public void setDeptService(DepartmentService deptService) {
		this.deptService = deptService;
	}

  
}
